import 'package:flutter/material.dart';
import 'dart:math' as math;

class AnimatedSectorProgress extends StatefulWidget {
  final Color ringColor;
  final Color sectorColor;
  final double ringWidth;
  final int animationDuration;
  final int minSectorAngle;
  final int maxSectorAngle;

  const AnimatedSectorProgress({
    super.key,
    this.ringColor = Colors.white,
    this.sectorColor = const Color(0xFFFFA500),
    this.ringWidth = 10.0,
    this.animationDuration = 1000,
    this.minSectorAngle = 20,
    this.maxSectorAngle = 180,
  });

  @override
  State<AnimatedSectorProgress> createState() => _AnimatedSectorProgressState();
}

class _AnimatedSectorProgressState extends State<AnimatedSectorProgress>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  double currentAngle = 0.0;
  double currentSectorLength = 0.0;

  @override
  void initState() {
    super.initState();

    // 确保最小角度小于最大角度
    int minAngle = widget.minSectorAngle;
    int maxAngle = widget.maxSectorAngle;
    if (minAngle >= maxAngle) {
      minAngle = 20;
      maxAngle = 180;
    }

    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: widget.animationDuration),
    )..repeat();

    _animation =
        Tween<double>(begin: 0, end: 720).animate(
          CurvedAnimation(parent: _controller, curve: Curves.linear),
        )..addListener(() {
          double value = _animation.value;
          // 修复：将int转换为double
          double angleRange = maxAngle.toDouble() - minAngle.toDouble();

          if (value <= 360) {
            // 阶段1: 0-360度
            currentAngle = value;
            currentSectorLength =
                minAngle.toDouble() + (angleRange * (value / 360));
          } else if (value <= 450) {
            // 阶段2: 360-450度
            currentAngle = value;
            currentSectorLength = maxAngle.toDouble();
          } else {
            // 阶段3: 450-720度
            currentAngle = value;
            double ratio = 1 - ((value - 450) / 270);
            currentSectorLength = minAngle.toDouble() + (angleRange * ratio);
          }

          setState(() {});
        });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _SectorPainter(
        ringColor: widget.ringColor,
        sectorColor: widget.sectorColor,
        ringWidth: widget.ringWidth,
        currentAngle: currentAngle,
        currentSectorLength: currentSectorLength,
      ),
    );
  }
}

class _SectorPainter extends CustomPainter {
  final Color ringColor;
  final Color sectorColor;
  final double ringWidth;
  final double currentAngle;
  final double currentSectorLength;

  // 以时钟12点为0度起点
  static const double angleOffset = -90;

  _SectorPainter({
    required this.ringColor,
    required this.sectorColor,
    required this.ringWidth,
    required this.currentAngle,
    required this.currentSectorLength,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = math.min(size.width, size.height) / 2 - ringWidth / 2;

    // 绘制圆环
    final ringPaint = Paint()
      ..color = ringColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = ringWidth
      ..isAntiAlias = true;

    canvas.drawCircle(center, radius, ringPaint);

    // 绘制扇形
    final sectorPaint = Paint()
      ..color = sectorColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = ringWidth
      ..strokeCap = StrokeCap.square
      ..isAntiAlias = true;

    final rect = Rect.fromCircle(center: center, radius: radius);

    // 转换角度为弧度
    final startAngle = (currentAngle + angleOffset) * math.pi / 180;
    final sweepAngle = currentSectorLength * math.pi / 180;

    canvas.drawArc(rect, startAngle, sweepAngle, false, sectorPaint);
  }

  @override
  bool shouldRepaint(covariant _SectorPainter oldDelegate) {
    return ringColor != oldDelegate.ringColor ||
        sectorColor != oldDelegate.sectorColor ||
        ringWidth != oldDelegate.ringWidth ||
        currentAngle != oldDelegate.currentAngle ||
        currentSectorLength != oldDelegate.currentSectorLength;
  }
}
